import api from '../../config/setting.js'
Page({
  data: {
    dataDict: {
      result: [
        {
          "id": 1,
          "name": "张铁蛋",
          "area": { "desc": "2单元3号楼", "name": "2#3" },
          "avatar": "/images/img/me.png"
        },  
        {
          "id": 2,
          "name": "刘亦菲",
          "area": { "desc": "4单元4号楼", "name": "4#4" },
          "avatar": "/images/img/me.png"
        }
      ],
      today_count: 3,
    }
  },
  onLoad(){
    this.refresh()
  },
  refresh(){
    //加载中
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    wx.request({
      url: api.collection,
      method: 'GET',
      success:(res)=>{
        console.log(res)
        if(res.data.code == 1){
          this.setData({
            dataDict: res.data
          })
        }
        else{
          wx.showToast({
            title: '网络加载失败！',
          })
        }
      },
      complete:()=>{
        wx.hideLoading()
      }
    })
  },
  doDeleteRow(e){
    wx.showModal({
      title: '您确定删除吗？',
      content: '',
      complete: (res) => {
        if (res.cancel) {
          return
        }  
        if (res.confirm) {
          var nid = e.currentTarget.dataset.nid
          wx.showLoading({
            title: '删除中...',
            mask: true
          })

          wx.request({
            url: api.collection+nid+'/',
            method: 'DELETE',
            success:()=>{
              //成功删除后刷新
              this.refresh()
            },
            complete:()=>{
              wx.hideLoading()
            }
          })
        }
      }
    })
  },
  onPullDownRefresh(){
    this.refresh()
  },
  bindToForm(){
    wx.navigateTo({
      url: '/pages/form/form',
    })
  },
  bindToStatistics(){
    wx.navigateTo({
      url: '/pages/statistics/statistics',
    })
  },
  onShow(){
    this.refresh()
  }
})